$(function () {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '工资曲线(单位：元)'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            data: ['工资']
        },
        xAxis: {
            data: [],
            axisLabel: {
                interval: 0,
            }
        },
        yAxis: {},
        series: [{
            name: '工资',
            type: 'line',
            data: [],
            itemStyle: {
                normal: {
                    label: {
                        show: true
                    }
                }
            }
        }]
    };
    $('#salary-dg').datagrid({
        striped: true,
        // pagination: true,
        rownumbers: true,
        fitColumns: true,
        scrollbarSize: 0,
        width: "auto",
        showFooter: true,
        url: 'mySalary',
        onBeforeSelect: function () {
            return false;
        },
        queryParams: {
            method: "findCurrYearByUserId"
        },
        columns: [[
            {field: 'remark', title: '备注', width: 20, align: 'center'},
            {field: 'salary', title: '工资', width: 15, align: 'right', halign: 'center'},
            {field: 'issueDate', title: '发放时间', width: 20, align: 'center'},
            {
                field: 'id', title: '操作', width: 15, formatter: function (value, row, index) {
                    let edit = '<button type="button" class="btn btn-primary btn-xs" name="edit" data-value="' + value + '" onclick="edit(this)"><i class="fa fa-pencil"></i>&nbsp;编辑</button>&nbsp;<button type="button" class="btn btn-danger btn-xs" name="del" data-value="' + value + '" onclick="deleteSalary(this)"><i class="fa fa-trash"></i>&nbsp;删除</button>';
                    return value === 0 ? "" : edit;

                }, align: 'center'
            },
        ]],
        toolbar: [
            {
                iconCls: 'icon-add',
                text: "新增",
                handler: function () {
                    $("#add").window('open');
                    $("#iconCls").focus();
                }
            }
            /*{
                iconCls: 'icon-remove',
                text: "批量删除",
                handler: function () {

                }
            }*/
        ],
        onLoadSuccess: function (data) {
            let footer = [];
            let salary = [];
            if (data.rows.length > 7) {
                option.xAxis.axisLabel.rotate = -20;
            } else {
                option.xAxis.axisLabel.rotate = 0;
            }
            $.each(data.rows, function (i, e) {
                footer.push(e.year + "年" + e.month + "月份工资");
                salary.push(e.salaryNum);
                option.xAxis.data = footer;
                option.series[0].data = salary;
            });
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    });

    laydate.render({
        elem: "#issueDate",
        value: new Date()
    });
    laydate.render({
        elem: "#issueDateEdit",
        value: new Date()
    });

    $("#add").window({
        width: 600,
        height: 300,
        title: '新增工资',
        iconCls: 'icon-add',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#edit").window({
        width: 600,
        height: 300,
        title: '编辑工资',
        iconCls: 'icon-save',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#addSalary").click(function () {
        addSalary("add");
    });

    $("#addSalaryContinue").click(function () {
        addSalary("continue");
    });

    $("#editSalary").on("click", function () {
        let id = $.trim($("#salaryId").val());
        let salary = $.trim($("#salaryEdit").val());
        let issueDate = $.trim($("#issueDateEdit").val());
        let remark = $.trim($("#remarkEdit").val());
        $.ajax({
            type: "post",
            url: "mySalary",
            data: {
                id: id,
                salary: salary,
                issueDate: issueDate,
                remark: remark,
                method: "update"
            },
            dataType: "json",
            success: function (data) {
                if (data.status === 20000) {
                    layer.msg(data.msg, {icon: 1})
                } else {
                    layer.msg(data.msg, {icon: 2})
                }
                $("#edit").window("close");
                $('#salary-dg').datagrid('reload');
            }
        });
    });

});

function addSalary(type) {
    let remark = $("#remark").val(),
        salary = $("#salary").val(),
        issueDate = $("#issueDate").val();
    if ($.trim(salary).length === 0) {
        return;
    }
    $.ajax({
        type: "post",
        url: "mySalary",
        data: {
            method: "save",
            remark: remark,
            salary: salary,
            issueDate: issueDate
        },
        dataType: "json",
        success: function (data) {
            if (data.status === 20000) {
                layer.msg(data.msg, {icon: 1})
            } else {
                layer.msg(data.msg, {icon: 2})
            }
            if (type === 'add') {
                $("#add").window("close");
            }
            $('#salary-dg').datagrid('reload');
        }
    });
}

function edit(obj) {
    let id = $(obj).attr("data-value");
    $("#edit").window('open');
    $.ajax({
        type: "get",
        url: "mySalary?method=findById&id=" + id,
        dataType: "json",
        success: function (data) {
            $("#salaryId").val(data.id);
            $("#salaryEdit").val(data.salary);
            $("#issueDateEdit").val(data.issueDate);
            $("#remarkEdit").val(data.remark);
        }
    });
}

function deleteSalary(obj) {
    layer.confirm("确认删除该条数据吗？", {icon: 3, title: "删除确认"}, function (index) {
        let id = $(obj).attr("data-value");
        $.ajax({
            type: "get",
            url: "mySalary?method=delete&id=" + id,
            dataType: "json",
            success: function (data) {
                if (data.status === 20000) {
                    layer.msg(data.msg, {icon: 1})
                } else {
                    layer.msg(data.msg, {icon: 2})
                }
                $('#salary-dg').datagrid('reload');
            }
        });
        layer.close(index);
    });
}
